package com.potenzacd.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenzacd.project.mapper.PdInfoReqsModuleMapper;
import com.potenzacd.project.pojo.*;
import com.potenzacd.project.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cuidongdong
 * @since 2021-01-26
 */
@Service
public class PdInfoReqsModuleService extends ServiceImpl<PdInfoReqsModuleMapper, PdInfoReqsModule> implements IPdInfoReqsModuleService {
    //产品信息
    @Autowired
    private IProductInfoService productInfoService;
    //历史产品信息
    @Autowired
    private IHisProductInfoService hisProductInfoService;

    //产品审核信息
    @Autowired
    private IProductApprovalInfoService productApprovalInfoService;

    //历史产品审核信息
    @Autowired
    private IHisProductApprovalInfoService hisProductApprovalInfoService;

    //产品产品功能需求模块
    @Autowired
    private IPdInfoReqsModuleService pdInfoReqsModuleService;

    //历史产品功能需求模块
    @Autowired
    private IHisPdInfoReqsModuleService hisPdInfoReqsModuleService;

    //产品功能需求
    @Autowired
    private IPdInfoReqsService pdInfoReqsService;
    //产品历史功能需求
    @Autowired
    private IHisPdInfoReqsService hisPdInfoReqsService;

    //产品信息通知
    @Autowired
    private IMessageNotificationProductService messageNotificationProductService;


    @Override
    public void save4Draft(ProductInfo productInfo) {
        //获取功能需求模块
        List<PdInfoReqsModule> pdInfoReqsModules = productInfo.getPdInfoReqsModules();
        //循环加入
        for(PdInfoReqsModule pdInfoReqsModule:pdInfoReqsModules){
            //维护属性
            pdInfoReqsModule.setCreateTime(new Date());
            pdInfoReqsModule.setUpdateTime(new Date());
            //维护关系
            pdInfoReqsModule.setProductInfoCode(productInfo.getCode());
            pdInfoReqsModuleService.save(pdInfoReqsModule);
            //添加功能需求
            List<PdInfoReqs> pdInfoReqses = pdInfoReqsModule.getPdInfoReqs();
            for (PdInfoReqs pdInfoReqs:pdInfoReqses){
                //维护关系
                pdInfoReqs.setPdInfoReqsModuleCode(pdInfoReqsModule.getCode());
            }
            //批量添加
            pdInfoReqsService.saveBatch(pdInfoReqses);
        }
        //修改产品的基本信息状态
        productInfo.setPdInfoReqsStatus(ProductInfo.Status.STATUS_DRAFT);
        //修改
        productInfoService.updateById(productInfo);
    }

    @Override
    public void save4Approval(ProductInfo productInfo) {
        List<PdInfoReqsModule> pdInfoReqsModules = productInfo.getPdInfoReqsModules();
        for (PdInfoReqsModule pdInfoReqsModule:pdInfoReqsModules){
            //维护属性
            pdInfoReqsModule.setCreateTime(new Date());
            pdInfoReqsModule.setUpdateTime(new Date());
            pdInfoReqsModule.setDeleted(2);//未删除
            //维护关系
            pdInfoReqsModule.setProductInfoCode(pdInfoReqsModule.getCode());
            this.pdInfoReqsModuleService.save(pdInfoReqsModule);
            List<PdInfoReqs> pdInfoReqs = pdInfoReqsModule.getPdInfoReqs();
            for (PdInfoReqs pdInfoReqs1:pdInfoReqs){
                pdInfoReqs1.setPdInfoReqsModuleCode(pdInfoReqsModule.getCode());
            }
            this.pdInfoReqsService.saveBatch(pdInfoReqs);
        }
        //修改产品的基本信息状态
        productInfo.setPdInfoReqsStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
        //保留历史业务信息
        ArrayList<HisPdInfoReqsModule> hisPdInfoReqsModules = new ArrayList<>();
        for(PdInfoReqsModule pdInfoReqsModule:pdInfoReqsModules){
            HisPdInfoReqsModule hisPdInfoReqsModule = new HisPdInfoReqsModule();
            //复制
            BeanUtils.copyProperties(pdInfoReqsModule,hisPdInfoReqsModule);
            hisPdInfoReqsModule.setHisProductInfoCode(productInfo.getCode());
            hisPdInfoReqsModules.add(hisPdInfoReqsModule);
        }
        this.hisPdInfoReqsModuleService.saveBatch(hisPdInfoReqsModules);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void edit4Draft(ProductInfo productInfo) {
        QueryWrapper<PdInfoReqsModule> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.lambda().eq(PdInfoReqsModule::getProductInfoCode,productInfo.getCode());
        //获取功能需求模块
        PdInfoReqsModule pdInfoReqsModule = this.pdInfoReqsModuleService.getOne(moduleQueryWrapper);

        QueryWrapper<PdInfoReqs> pdInfoReqsQueryWrapper = new QueryWrapper<>();
        pdInfoReqsQueryWrapper.lambda().eq(PdInfoReqs::getPdInfoReqsModuleCode,pdInfoReqsModule.getCode());
        //先删除
        this.pdInfoReqsService.remove(pdInfoReqsQueryWrapper);
        this.pdInfoReqsModuleService.remove(moduleQueryWrapper);

        //获取功能需求模块
        List<PdInfoReqsModule> pdInfoReqsModules = productInfo.getPdInfoReqsModules();
        //循环加入
        for (PdInfoReqsModule pdInfoReqsModule1:pdInfoReqsModules){
            //维护属性
            pdInfoReqsModule1.setCreateTime(new Date());
            pdInfoReqsModule1.setUpdateTime(new Date());
            pdInfoReqsModule1.setDeleted(2);
            //维护关系
            pdInfoReqsModule1.setProductInfoCode(productInfo.getCode());
            this.pdInfoReqsModuleService.save(pdInfoReqsModule1);
            //添加功能需求
            List<PdInfoReqs> pdInfoReqs = pdInfoReqsModule1.getPdInfoReqs();
            for (PdInfoReqs pdInfoReqs1:pdInfoReqs){
                pdInfoReqs1.setCreateTime(new Date());
                pdInfoReqs1.setUpdateTime(new Date());
                pdInfoReqs1.setDeleted(2);
                pdInfoReqs1.setPdInfoReqsModuleCode(pdInfoReqsModule1.getCode());
            }
            //批量添加需求
            this.pdInfoReqsService.saveBatch(pdInfoReqs);
        }
        productInfo.setStatus(ProductInfo.Status.STATUS_CHANGING_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
    }

    @Override
    public void edit4Approval(ProductInfo productInfo) {
        QueryWrapper<PdInfoReqsModule> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.lambda().eq(PdInfoReqsModule::getProductInfoCode,productInfo.getCode());
        //获取功能需求模块
        PdInfoReqsModule pdInfoReqsModule = this.pdInfoReqsModuleService.getOne(moduleQueryWrapper);

        QueryWrapper<PdInfoReqs> pdInfoReqsQueryWrapper = new QueryWrapper<>();
        pdInfoReqsQueryWrapper.lambda().eq(PdInfoReqs::getPdInfoReqsModuleCode,pdInfoReqsModule.getCode());
        //先删除
        this.pdInfoReqsService.remove(pdInfoReqsQueryWrapper);
        this.pdInfoReqsModuleService.remove(moduleQueryWrapper);
        //获取功能需求模块
        List<PdInfoReqsModule> pdInfoReqsModules = productInfo.getPdInfoReqsModules();
        //循环加入
        for (PdInfoReqsModule pdInfoReqsModule1:pdInfoReqsModules){
            //维护属性
            pdInfoReqsModule1.setCreateTime(new Date());
            pdInfoReqsModule1.setUpdateTime(new Date());
            pdInfoReqsModule1.setDeleted(2);
            //维护关系
            pdInfoReqsModule1.setProductInfoCode(productInfo.getCode());
            this.pdInfoReqsModuleService.save(pdInfoReqsModule1);
            //添加功能需求
            List<PdInfoReqs> pdInfoReqs = pdInfoReqsModule1.getPdInfoReqs();
            for (PdInfoReqs pdInfoReqs1:pdInfoReqs){
                pdInfoReqs1.setCreateTime(new Date());
                pdInfoReqs1.setUpdateTime(new Date());
                pdInfoReqs1.setDeleted(2);
                pdInfoReqs1.setPdInfoReqsModuleCode(pdInfoReqsModule1.getCode());
            }
            //批量添加需求
            this.pdInfoReqsService.saveBatch(pdInfoReqs);
        }
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
        //保留历史业务信息
        ArrayList<HisPdInfoReqsModule> hisPdInfoReqsModules = new ArrayList<>();
        for(PdInfoReqsModule pdInfoReqsModule1:pdInfoReqsModules){
            HisPdInfoReqsModule hisPdInfoReqsModule = new HisPdInfoReqsModule();
            //复制
            BeanUtils.copyProperties(pdInfoReqsModule1,hisPdInfoReqsModule);
            hisPdInfoReqsModule.setHisProductInfoCode(productInfo.getCode());
            hisPdInfoReqsModules.add(hisPdInfoReqsModule);
        }
        this.hisPdInfoReqsModuleService.saveBatch(hisPdInfoReqsModules);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void pass(ProductApprovalInfo productApprovalInfo) {
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        //维护属性
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        //设置状态  1--基本信息 2---业务流程  3--功能需求
        productApprovalInfo.setType(2);
        //设置审核是否通过
        productApprovalInfo.setPass(1);
        this.productApprovalInfoService.save(productApprovalInfo);

        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productApprovalInfo.getProductInfoCode());
        //获取基本信息
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        //设置状态
        productInfo.setStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        //修改
        this.productInfoService.updateById(productInfo);

        //保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        //复制
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        //维护属性
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        //入库
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void notPass(ProductApprovalInfo productApprovalInfo) {
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        //维护属性
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        //设置状态  1--基本信息 2---业务流程  3--功能需求
        productApprovalInfo.setType(2);
        //设置审核是否通过
        productApprovalInfo.setPass(2);
        this.productApprovalInfoService.save(productApprovalInfo);

        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productApprovalInfo.getProductInfoCode());
        //获取基本信息
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        //设置状态
        productInfo.setStatus(ProductInfo.Status.STATUS_APPROVAL_FAIL);
        //修改
        this.productInfoService.updateById(productInfo);

        //保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        //复制
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        //维护属性
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        //入库
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void revise(ProductInfo productInfo) {
        QueryWrapper<PdInfoReqsModule> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.lambda().eq(PdInfoReqsModule::getProductInfoCode,productInfo.getCode());
        //获取功能需求模块
        PdInfoReqsModule pdInfoReqsModule = this.pdInfoReqsModuleService.getOne(moduleQueryWrapper);

        QueryWrapper<PdInfoReqs> pdInfoReqsQueryWrapper = new QueryWrapper<>();
        pdInfoReqsQueryWrapper.lambda().eq(PdInfoReqs::getPdInfoReqsModuleCode,pdInfoReqsModule.getCode());
        //先删除
        this.pdInfoReqsService.remove(pdInfoReqsQueryWrapper);
        this.pdInfoReqsModuleService.remove(moduleQueryWrapper);
        //获取功能需求模块
        List<PdInfoReqsModule> pdInfoReqsModules = productInfo.getPdInfoReqsModules();
        //循环加入
        for (PdInfoReqsModule pdInfoReqsModule1:pdInfoReqsModules){
            //维护属性
            pdInfoReqsModule1.setCreateTime(new Date());
            pdInfoReqsModule1.setUpdateTime(new Date());
            pdInfoReqsModule1.setDeleted(2);
            //维护关系
            pdInfoReqsModule1.setProductInfoCode(productInfo.getCode());
            this.pdInfoReqsModuleService.save(pdInfoReqsModule1);
            //添加功能需求
            List<PdInfoReqs> pdInfoReqs = pdInfoReqsModule1.getPdInfoReqs();
            for (PdInfoReqs pdInfoReqs1:pdInfoReqs){
                pdInfoReqs1.setCreateTime(new Date());
                pdInfoReqs1.setUpdateTime(new Date());
                pdInfoReqs1.setDeleted(2);
                pdInfoReqs1.setPdInfoReqsModuleCode(pdInfoReqsModule1.getCode());
            }
            //批量添加需求
            this.pdInfoReqsService.saveBatch(pdInfoReqs);
        }
        productInfo.setStatus(ProductInfo.Status.STATUS_CHANGING_WAIT_APPROVAL);
        this.productInfoService.updateById(productInfo);
        //保留历史业务信息
        ArrayList<HisPdInfoReqsModule> hisPdInfoReqsModules = new ArrayList<>();
        for(PdInfoReqsModule pdInfoReqsModule1:pdInfoReqsModules){
            HisPdInfoReqsModule hisPdInfoReqsModule = new HisPdInfoReqsModule();
            //复制
            BeanUtils.copyProperties(pdInfoReqsModule1,hisPdInfoReqsModule);
            hisPdInfoReqsModule.setHisProductInfoCode(productInfo.getCode());
            hisPdInfoReqsModules.add(hisPdInfoReqsModule);
        }
        this.hisPdInfoReqsModuleService.saveBatch(hisPdInfoReqsModules);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void abort(String code) {
        QueryWrapper<PdInfoReqsModule> pdInfoReqsModuleQueryWrapper = new QueryWrapper<>();
        pdInfoReqsModuleQueryWrapper.lambda().eq(PdInfoReqsModule::getCode,code);
        PdInfoReqsModule pdInfoReqsModule = this.pdInfoReqsModuleService.getOne(pdInfoReqsModuleQueryWrapper);
        //获取基本信息标识
        String productInfoCode = pdInfoReqsModule.getProductInfoCode();
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfo.setUpdateTime(new Date());
        //设置状态
        productInfo.setStatus(ProductInfo.Status.STATUS_ABORT);
        this.productInfoService.updateById(productInfo);
        //3:发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setKind(2);//业务流程信息
        messageNotifiProduct.setType(1);//待审核
        String title=productInfo.getName()+"的业务流程信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content="领导,您有一个产品业务流程需要审核,大致如下:"+"产品的名称"+productInfo.getName()+"请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfo.getCode());
        messageNotificationProductService.save(messageNotifiProduct);

    }

    @Override
    public PdInfoReqsModule detail(String code) {
        QueryWrapper<PdInfoReqsModule> pdInfoReqsModuleQueryWrapper = new QueryWrapper<>();
        pdInfoReqsModuleQueryWrapper.lambda().eq(PdInfoReqsModule::getCode,code);
        PdInfoReqsModule pdInfoReqsModule = this.pdInfoReqsModuleService.getOne(pdInfoReqsModuleQueryWrapper);
        List<PdInfoReqs> pdInfoReqs = pdInfoReqsModule.getPdInfoReqs();
        for (PdInfoReqs pdInfoReqs1:pdInfoReqs){
            QueryWrapper<PdInfoReqs> pdInfoReqsQueryWrapper = new QueryWrapper<>();
            pdInfoReqsQueryWrapper.lambda().eq(PdInfoReqs::getPdInfoReqsModuleCode,code);
            this.pdInfoReqsService.list(pdInfoReqsQueryWrapper);
        }
        return pdInfoReqsModule;
    }
}
